package com.hhh.Service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hhh.Dto.SetmealDto;
import com.hhh.ExceptionHandle.SetmealException;
import com.hhh.Mapper.SetmealMapper;
import com.hhh.Pojo.Category;
import com.hhh.Pojo.Dish;
import com.hhh.Pojo.Setmeal;
import com.hhh.Pojo.SetmealDish;
import com.hhh.Service.CategoryService;
import com.hhh.Service.SetmealDishSerivce;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.function.Consumer;
import java.util.function.Function;

@Service
public class SetmealService implements com.hhh.Service.SetmealService {
    @Autowired
    SetmealMapper setmealMapper;

    @Autowired
    CategoryService categoryService;

    @Autowired
    SetmealDishSerivce setmealDishSerivce;

    //    根据id查询套餐
    @Override
    @Transactional
    public SetmealDto SelectById(long id) {
        SetmealDto setmealDto = new SetmealDto();
        Setmeal setmeal = setmealMapper.selectById(id);
        BeanUtils.copyProperties(setmeal, setmealDto);
        setmealDto.setSetmealDishes(setmealDishSerivce.SelectBySetmealId(id));
        return setmealDto;
    }

    //    根据CategoryId查询数据条数
    @Override
    public long SelectCount(long CategoryId) {
        QueryWrapper<Setmeal> wrapper = new QueryWrapper<>();
        wrapper.eq("category_id", CategoryId);
        return setmealMapper.selectCount(wrapper);
    }

    //    分页查询
//    因为前端页面要展示的是分类的名字，
//    所以要用SetmealDto类返回
    @Override
    @Transactional
    public IPage<SetmealDto> SelectAll(long page, long pageSize, String name) {
        IPage<Setmeal> iPage = new Page<>();
        iPage.setSize(pageSize);
        iPage.setCurrent(page);
        QueryWrapper<Setmeal> wrapper = new QueryWrapper<>();
        if (name != null) {
            wrapper.eq("name", name);
        }
        IPage<Setmeal> setmealIPage = setmealMapper.selectPage(iPage, wrapper);
        IPage<SetmealDto> setmealDtoIPage = new Page<>();

//        把setmealIPage转化成setmealDtoIPage，除了records
        BeanUtils.copyProperties(setmealIPage, setmealDtoIPage, "records");
        List<SetmealDto> setmealDtos = setmealIPage.getRecords().stream().map(new Function<Setmeal, SetmealDto>() {
            @Override
            public SetmealDto apply(Setmeal setmeal) {
//                把records里面的Setmeal转化成SetmealDto
                SetmealDto setmealDto = new SetmealDto();
                Category category = categoryService.SelectById(setmeal.getCategoryId());
                setmealDto.setCategoryName(category.getName());
                BeanUtils.copyProperties(setmeal, setmealDto);
                return setmealDto;
            }
        }).toList();

        return setmealDtoIPage.setRecords(setmealDtos);
    }


    //    添加套餐
    @Override
    @Transactional
    public Integer Insert(SetmealDto setmealDto) {
        setmealMapper.insert(setmealDto);
        List<SetmealDish> setmealDishList = setmealDto.getSetmealDishes();
        int num = 0;
        int len = setmealDishList.size();
        int returnnum = 1;
        while (num < len && setmealDishList.get(num) != null) {
            SetmealDish setmealDish = setmealDishList.get(num);
            setmealDish.setSetmealId(setmealDto.getId());
            returnnum += setmealDishSerivce.Insert(setmealDish);
            num++;
        }
        if (returnnum != len)
            return 0;
        return returnnum;
    }

    //    删除套餐
    @Override
    @Transactional
    public Integer DeleteById(List<Long> ids) {
        int num = 1;
        for (long id : ids) {
//            判断是否停售
            Setmeal setmeal = setmealMapper.selectById(id);
            if (setmeal.getStatus() != 0)
                return 0;
            num += setmealMapper.deleteById(id);
            SetmealDish setmealDish = new SetmealDish();
            setmealDish.setSetmealId(id);
            setmealDishSerivce.Delete(setmealDish);
        }
        return num;
    }

    //    更改setmeal的status
    @Override
    public Integer Updatestatus(Integer status, List<Long> ids) {
        int num = 0;
        for (long id : ids) {
            Setmeal setmeal = new Setmeal();
            setmeal.setId(id);
            setmeal.setStatus(status);
            num += setmealMapper.updateById(setmeal);
        }
        return num;
    }

    //    更新套餐
    @Override
    @Transactional
    public Integer Update(SetmealDto setmealDto) {
        Setmeal setmeal = setmealMapper.selectById(setmealDto.getId());
        if (setmeal.getStatus() != 0)
            throw new SetmealException("要先停售才能修改套餐");
        int num = setmealMapper.updateById(setmealDto);
        setmealDishSerivce.Update(setmealDto.getSetmealDishes());
        return num;
    }
}
